import React from 'react';
import { Form, Input, Button, Card, Typography, Space, message } from 'antd';
import { UserOutlined, LockOutlined, TeamOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { login } from '../../store/slices/userSlice';
import type { AppDispatch } from '../../store';
import './Login.css';

const { Title, Text } = Typography;

interface LoginForm {
  username: string;
  password: string;
}

export const Login: React.FC = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch<AppDispatch>();
  const [form] = Form.useForm();
  const [loading, setLoading] = React.useState(false);

  const onFinish = async (values: LoginForm) => {
    setLoading(true);

    try {
      await new Promise(resolve => setTimeout(resolve, 1000));

      // 验证逻辑
      if ((values.username === 'admin' && values.password === 'admin123') ||
        (values.username === 'xyxyxy111' && values.password === 'xyxyxy616161')) {

        const userData = {
          id: values.username === 'admin' ? '1' : '2',
          username: values.username,
          role: values.username === 'admin' ? 'admin' : 'user' as const,
          department: values.username === 'admin' ? '实验室管理中心' : '计算机学院',
          email: `${values.username}@lab.edu.cn`,
          phone: '13800138000'
        };

        // 使用 Redux 存储用户信息
        dispatch(login(userData));
        message.success('登录成功！');
        navigate('/dashboard');
      } else {
        message.error('用户名或密码错误！');
      }
    } catch (error) {
      message.error('登录失败，请重试！');
    } finally {
      setLoading(false);
    }
  };

  const fillDemoAccount = (type: 'admin' | 'user') => {
    if (type === 'admin') {
      form.setFieldsValue({
        username: 'admin',
        password: 'admin123'
      });
    } else {
      form.setFieldsValue({
        username: 'xyxyxy111',
        password: 'xyxyxy616161'
      });
    }
  };

  return (
    <div className="login-container">
      <div className="login-background">
        <Card className="login-card">
          <div className="login-header">
            <TeamOutlined className="login-icon" />
            <Title level={2} className="login-title">
              实验室资源管理系统
            </Title>
            <Text type="secondary">请登录您的账户</Text>
          </div>

          <Form
            form={form}
            name="login"
            onFinish={onFinish}
            autoComplete="off"
            size="large"
            className="login-form"
          >
            <Form.Item
              name="username"
              rules={[
                { required: true, message: '请输入用户名!' },
                { min: 3, message: '用户名至少3个字符!' }
              ]}
            >
              <Input
                prefix={<UserOutlined className="input-icon" />}
                placeholder="用户名"
                allowClear
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[
                { required: true, message: '请输入密码!' },
                { min: 6, message: '密码至少6个字符!' }
              ]}
            >
              <Input.Password
                prefix={<LockOutlined className="input-icon" />}
                placeholder="密码"
                allowClear
              />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                className="login-button"
                loading={loading}
                block
              >
                登录
              </Button>
            </Form.Item>
          </Form>

          <div className="demo-accounts">
            <Text type="secondary">演示账户：</Text>
            <Space size="middle" style={{ marginTop: 8 }}>
              <Button
                type="link"
                size="small"
                onClick={() => fillDemoAccount('admin')}
                className="demo-button"
              >
                管理员 (admin/admin123)
              </Button>
            </Space>
          </div>
        </Card>
      </div>
    </div>
  );
};

export default Login;